package com.huluobo.high3videoproject.mvp.fullscreen.presenter;

import com.huluobo.base.view.BasePresenter;
import com.huluobo.high3videoproject.bean.CommentBeanResult;
import com.huluobo.high3videoproject.bean.FollowBean;
import com.huluobo.high3videoproject.bean.LikeBean;
import com.huluobo.high3videoproject.bean.SendCommentBeanResult;
import com.huluobo.high3videoproject.bean.VideoBeanResult;
import com.huluobo.high3videoproject.mvp.fullscreen.model.VideoFullscreenModel;
import com.huluobo.high3videoproject.mvp.video.model.VideoModel;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class VideoFullscreenPresenter extends BasePresenter<VideoFullscreenContract.View> implements VideoFullscreenContract.Presenter {
    private VideoFullscreenModel videoModel = new VideoFullscreenModel();

    @Override
    public void getCommentList(int videoId) {
        videoModel.getCommentList(videoId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommentBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(CommentBeanResult commentBeanResult) {
                        if (isAttach()) {
                            getView().onGetCommentListSuccess(commentBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onGetCommentListFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void sendComment(String msg, int videoId) {
        videoModel.sendComment(msg, videoId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommentBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(CommentBeanResult sendCommentBeanResult) {
                        if (isAttach()) {
                            getView().onSendCommentSuccess(sendCommentBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onSendCommentFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getVideoList(int page, int size) {
        videoModel.getVideoList(page, size)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(VideoBeanResult videoBeanResult) {
                        if (isAttach()) {
                            getView().onGetVideoListSuccess(videoBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onGetVideoListFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void followUser(String authname) {
        videoModel.followUser(authname).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FollowBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(FollowBean followBean) {
                        if (isAttach()) {
                            getView().onFollowSuccess(followBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onFollowFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void likeVideo(int videoId) {
        videoModel.likeVideo(videoId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LikeBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(LikeBean likeBean) {
                        if (isAttach()) {
                            getView().onLikeVideoSuccess(likeBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onLikeVideoFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void noLikeVideo(int videoId) {
        videoModel.noLikeVideo(videoId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LikeBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(LikeBean likeBean) {
                        if (isAttach()) {
                            getView().onNoLikeVideoSuccess(likeBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onNoLikeVideoFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
